# 公用自定义工具类
import functools

from flask import session, current_app, g

from info.models import User


def rank_list_num(index):
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"

    return ""


def news_review_status(status):
    """
    0代表审核通过，1代表审核中，-1代表审核不通过 
    """
    if status == 0:
        return "审核通过"
    elif status == 1:
        return "审核中"
    elif status == -1:
        return "审核不通过"

    return ""


def user_login_data(f):
    # 使用functools.wraps 去修饰内层函数，可以保持当前装饰器装饰的函数的__name__的值不变
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id", None)
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 将查询出来的数据赋值给g变量
        g.user = user
        return f(*args, **kwargs)
    return wrapper
